

package com.maoshi.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maoshi.shop.bean.model.Category;
import com.maoshi.shop.bean.model.Product;
import com.maoshi.shop.common.annotation.SysLog;
import com.maoshi.shop.service.CategoryService;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 分类管理
 * @author lgh
 *
 */
@RestController
@RequestMapping("/prod/category/")
public class CategoryController {

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private ProductService productService;

	/**
	 * 获取分类信息
	 */
	@GetMapping("/info/{categoryId}")
	public ResponseEntity<Category> info(@PathVariable("categoryId") Long categoryId){
		Category category = categoryService.getById(categoryId);
		return ResponseEntity.ok(category);
	}

	/**
	 * 保存分类
	 */
	@SysLog("保存分类")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('prod:category:save')")
	public ResponseEntity<Void> save(@RequestBody Category category){
		category.setShopId(Constant.PLATFORM_SHOP_ID);
		category.setRecTime(new Date());
		category.setGrade(getGradeByParentId(category.getParentId()));
		categoryService.saveCategroy(category);
		return ResponseEntity.ok().build();
	}

	/**
	 * 更新分类
	 */
	@SysLog("更新分类")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('prod:category:update')")
	public ResponseEntity<String> update(@RequestBody Category category){
		category.setShopId(Constant.PLATFORM_SHOP_ID);
		Category categoryDB = categoryService.getById(category.getCategoryId());
		if (Objects.equals(categoryDB.getParentId(), Constant.CATEGORY_ID) && !Objects.equals(category.getParentId(), Constant.CATEGORY_ID)){
			return ResponseEntity.badRequest().body("一级分类不能改为二级分类");
		}else if(Objects.equals(category.getParentId(), Constant.CATEGORY_ID) && !Objects.equals(categoryDB.getParentId(), Constant.CATEGORY_ID)){
			return ResponseEntity.badRequest().body("二级分类不能改为一级分类");
		}
		category.setGrade(getGradeByParentId(category.getParentId()));
		category.setOldCategoryName(categoryDB.getCategoryName());
		categoryService.updateCategroy(category);
		return ResponseEntity.ok().build();
	}

	/**
	 * 删除分类
	 */
	@SysLog("删除分类")
	@DeleteMapping("/{categoryId}")
	@PreAuthorize("@pms.hasPermission('prod:category:delete')")
	public ResponseEntity<String> delete(@PathVariable("categoryId") Long categoryId){
		if (categoryService.count(new LambdaQueryWrapper<Category>().eq(Category::getParentId,categoryId)) >0) {
			return ResponseEntity.badRequest().body("请删除子分类，再删除该分类");
		}
		int categoryProdCount = productService.count(new LambdaQueryWrapper<Product>().eq(Product::getCategoryId, categoryId).ne(Product::getStatus, -1));
		if (categoryProdCount>0){
			return ResponseEntity.badRequest().body("该分类下还有商品，请先删除该分类下的商品");
		}
		Category category = categoryService.getById(categoryId);
		categoryService.deleteCategroy(category);
		return ResponseEntity.ok().build();
	}

	/**
	 * 所有的
	 */
	@GetMapping("/listCategory")
	public ResponseEntity<List<Category>> listCategory(@RequestParam Integer maxGrade){

		return ResponseEntity.ok(categoryService.list(new LambdaQueryWrapper<Category>()
														.le(Category::getGrade, maxGrade)
														.eq(Category::getShopId, Constant.PLATFORM_SHOP_ID)
														.orderByAsc(Category::getSeq)));
	}

	private int getGradeByParentId(Long parentId) {
		// 如果上级为id为0，则设置分类等级为0
		if (Objects.equals(parentId,0L)) {
			return 0;
		}
		Category parentCategory = categoryService.getById(parentId);
		return parentCategory.getGrade() + 1;
	}

	/**
	 * 平台的分类
	 */
	@GetMapping("/platformCategory")
	public ResponseEntity<List<Category>> platformCategory(@RequestParam Integer maxGrade){

		return ResponseEntity.ok(categoryService.list(new LambdaQueryWrapper<Category>()
				.le(Category::getGrade, maxGrade)
				.eq(Category::getShopId, Constant.PLATFORM_SHOP_ID)
				.orderByAsc(Category::getSeq)));
	}

}
